package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.ResourceMapper;
import com.kkb.pojo.ResourceExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResourceService {

    @Resource
    private ResourceMapper resourceMapper;

    /**
     * 多条件分页查询菜单资源
     * @param pageNum
     * @param pageSize
     * @param likeName
     * @return
     */
    @Transactional(readOnly = true)
    public PageInfo<com.kkb.pojo.Resource> selectResourceInPages(int pageNum, int pageSize, String likeName){
        ResourceExample resourceExample = new ResourceExample();
        ResourceExample.Criteria criteria = resourceExample.createCriteria();
        if (likeName!=null && !"".equals(likeName)){
            criteria.andResourceNameLike("%"+likeName+"%");
        }
        criteria.andResourceStateEqualTo(0);
        PageHelper.startPage(pageNum,pageSize);
        List<com.kkb.pojo.Resource> resources = resourceMapper.selectByExample(resourceExample);
        return new PageInfo<>(resources);
    }

    /**
     * 添加一个菜单资源
     * @param resource
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addResource(com.kkb.pojo.Resource resource){
        if (resource!=null) {
            return resourceMapper.insertSelective(resource);
        }
        return 0;
    }

    /**
     * 主键查询单个菜单资源，用于加载单个菜单资源的更新页面
     * @param resourceId
     * @return
     */
    @Transactional(readOnly = true)
    public com.kkb.pojo.Resource selectResourceInfo(Integer resourceId){
        if (resourceId==null){
            return null;
        }
        return resourceMapper.selectByPrimaryKey(resourceId);
    }

    /**
     * 根据主键更新菜单资源
     * @param resource
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateResource(com.kkb.pojo.Resource resource){
        return resourceMapper.updateByPrimaryKeySelective(resource);
    }

    /**
     * 查询父菜单资源
     * @return
     */
    @Transactional(readOnly = true)
    public List<com.kkb.pojo.Resource> selectParentList(){
        return resourceMapper.selectByExample(null);
    }


    /**
     * 逻辑删除单个菜单资源
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int logicalDeleteResource(Integer id){
        com.kkb.pojo.Resource resource = new com.kkb.pojo.Resource();
        resource.setResourceId(id);
        resource.setResourceState(1);
        return resourceMapper.updateByPrimaryKeySelective(resource);
    }

    /**
     * 逻辑删除多个菜单资源
     * @param list
     * @return
     */
    public int logicDeleteResourceList(List<Integer> list){
        if (list!=null){
            return resourceMapper.logicalDeleteResource(list);
        }
        return 0;
    }
}
